package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 分页条件查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
       //dto是否为null
        if (dto==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //对分页参数进行校验
        dto.checkParam();
        //创建分页对象page
        Page<AdSensitive> page=new Page<>(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())){
            //判断传入条件getName是否为null，为则不添加条件
            lambdaQueryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }


        //分页查询
        IPage<AdSensitive> iPage = page(page, lambdaQueryWrapper);
        //返回结构集
       ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
       //添加分页查询数据
       result.setData(iPage.getRecords());

        return result;
    }

    /**
     * 添加数据
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //判断是否为空
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        save(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id修改数据
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {

        if (adSensitive==null||adSensitive.getId()==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     * @param
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        if (id==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //通过id查询对象，判断是否存在
        AdSensitive adSensitive = getById(id);
        if (adSensitive==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //删除对象
        removeById(id);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }



}
